﻿using CrystalDecisions.CrystalReports.Engine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QRCode
{
    public partial class frmQRCode : Form
    {
        public frmQRCode()
        {
            InitializeComponent();
			this.dataGV.AutoGenerateColumns = false;
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
			try
			{
				OpenFileDialog openFileDialog = new OpenFileDialog();
				openFileDialog.Multiselect = false;
				openFileDialog.Filter = "Excel文件|*.xls;*.xlsx";
				if (DialogResult.OK == openFileDialog.ShowDialog())
				{
					if (!string.IsNullOrEmpty(openFileDialog.FileName) && File.Exists(openFileDialog.FileName))
					{
						this.GetDetail(openFileDialog.FileName);
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("获取记录出错!或者是未安装OFFICE插件，请确认安装！" + ex.Message);
				//try
				//{
				//	Process.Start(Application.StartupPath + "\\AccessDatabaseEngine.exe");
				//}
				//catch (Exception ex1)
				//{
				//}
			}
		}

		private void GetDetail(string path)
		{
			DataSet dataSet = this.ExcelToDS(path);
			if (dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0)
			{
				DataTable dt = dataSet.Tables[0];
				DataEntity[] dataSource = DataEntity.DataTableToEntity(dt);
				this.dataGV.DataSource = dataSource;
				foreach (DataGridViewColumn dataGridViewColumn in this.dataGV.Columns)
				{
					if (dataGridViewColumn.Name == this.pack.Name)
					{
						dataGridViewColumn.ReadOnly = false;
					}
					else
					{
						dataGridViewColumn.DefaultCellStyle.BackColor = Color.Silver;
						dataGridViewColumn.ReadOnly = true;
					}
				}
			}
		}

		private DataSet ExcelToDS(string Path)
		{
			string text = "Provider=Microsoft.Ace.OleDb.12.0;data source=" + Path + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
			OleDbConnection oleDbConnection = new OleDbConnection(text);
			oleDbConnection.Open();
			string selectCommandText = "select * from [lblDN$]";
			OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(selectCommandText, text);
			DataSet dataSet = new DataSet();
			oleDbDataAdapter.Fill(dataSet, "table1");
			oleDbConnection.Dispose();
			return dataSet;
		}

		private void dataGV_CellValidated(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				if (this.dataGV.Columns[e.ColumnIndex].Name == this.pack.Name)
				{
					decimal num = 0m;
					decimal num2 = 0m;
					decimal.TryParse(this.dataGV[this.qty.Name, e.RowIndex].Value.ToString(), out num);
					decimal.TryParse(this.dataGV[this.pack.Name, e.RowIndex].Value.ToString(), out num2);
					if (num2 <= 0m || num <= 0m)
					{
						MessageBox.Show("数量/包装必须大于0");
					}
					else if (num2 > num)
					{
						this.dataGV[this.total.Name, e.RowIndex].Value = 1;
					}
					else
					{
						int num3 = (int)(num / num2);
						decimal num4 = 0m;
						if (num % num2 > 0m)
						{
							num4 = num - num2 * num3;
							num3++;
						}
						this.dataGV[this.total.Name, e.RowIndex].Value = num3;
						this.dataGV[this.remainder.Name, e.RowIndex].Value = num4;
					}
				}
			}
			catch (Exception var_4_1B5)
			{
				MessageBox.Show("计算出错!");
			}
		}

		private void dataGV_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
			MessageBox.Show("格式出错!");
		}

		private void dataGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
		}

        private void btnPrint_Click(object sender, EventArgs e)
        {
			if (this.dataGV.Rows.Count <= 0)
			{
				MessageBox.Show("无效数据!");
			}
			else
			{
				DataEntity[] array = (DataEntity[])this.dataGV.DataSource;
				DataEntity[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					DataEntity dataEntity = array2[i];
					if (string.IsNullOrEmpty(dataEntity.PONo))
					{
						MessageBox.Show("PONo不能为空！");
						return;
					}
					if (string.IsNullOrEmpty(dataEntity.PartNum))
					{
						MessageBox.Show("物料不能为空！");
						return;
					}
					if (string.IsNullOrEmpty(dataEntity.DNNo))
					{
						MessageBox.Show("送货单不能为空！");
						return;
					}
					if (dataEntity.Qty <= 0m)
					{
						MessageBox.Show("数量不能小于0！");
						return;
					}
					if (dataEntity.Proc <= 0m)
					{
						MessageBox.Show("数量/包装不能小于0！");
						return;
					}
					if (dataEntity.Total <= 0)
					{
						MessageBox.Show("包装数不能小于0！");
						return;
					}
				}
				this.btnPrint.Enabled = false;
				try
				{
					DataTable dataTable = DataEntity.EntityToDataTable(array);
					dataTable.TableName = "QRCode";
					if (dataTable == null || dataTable.Rows.Count <= 0)
					{
						return;
					}
					ReportDocument reportDocument = new ReportDocument();
					dataTable.WriteXml(Application.StartupPath + "\\QRCOde.xml", XmlWriteMode.WriteSchema);
					dataTable.WriteXml(Application.StartupPath + "\\QRCOde.xml", XmlWriteMode.WriteSchema);
					File.Copy(Application.StartupPath + "\\QRCodePrint.rpt", Application.StartupPath + "\\QRCode\\QRCodePrint.rpt", true);
					for (int j = 0; j < 100; j++)
					{
						Application.DoEvents();
						Thread.Sleep(30);
					}
					reportDocument.Load(Application.StartupPath + "\\QRCode\\QRCodePrint.rpt");
					reportDocument.SetDataSource(dataTable);
					PrintShowDialog printShowDialog = new PrintShowDialog();
					printShowDialog.ShowReport(reportDocument, "打印");
				}
				catch (Exception var_6_242)
				{
					MessageBox.Show("打印出错！请确认是否安装水晶报表插件，请安装！");
					//try
					//{
					//	Process.Start(Application.StartupPath + "\\CRRuntime_32bit_13_0_3.msi");
					//}
					//catch (Exception var_7_269)
					//{
					//}
				}
				this.btnPrint.Enabled = true;
			}
		}
    }
}
